package org.maxkey.portal.api.controller;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.commons.codec.Charsets;
import org.apache.commons.io.IOUtils;
import org.maxkey.portal.api.handler.WebAuthenticationHandler;
import org.maxkey.portal.core.authentication.UsernamePasswordCredential;
import org.maxkey.portal.core.cam.*;
import org.maxkey.portal.service.NavigationService;
import org.maxkey.portal.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private NavigationService navigationService;

    @Autowired
    @Qualifier("webAuthenticationHandler")
    private WebAuthenticationHandler webAuthenticationHandler;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping(value = LoginConstants.LOGIN_ACTION , produces=Constants.APPLICATION_JSON_UTF8)
    public AuthResult login(@RequestBody UsernamePasswordCredential credential) {
        if (!UsernamePasswordCredential.class.isInstance(credential)) {
            log.debug("Credential is not one of username/password and is not accepted by handler [{}]", credential);
            return new AuthResult(Constants.VALIDATA_CREDENTIAL);
        }
        return webAuthenticationHandler.authentication(request, credential);
    }

    @PostMapping(value = LoginConstants.LOGINOUT_ACTION , produces=Constants.APPLICATION_JSON_UTF8)
    public ResultData logout() {
        String access_token=request.getParameter(Constants.CURRENT_ACCESS_TOKEN);
        if(access_token!=null){
            redisTemplate.delete(access_token);
        }else{
            access_token=request.getHeader(Constants.CURRENT_ACCESS_TOKEN);
            if(access_token!=null){
                redisTemplate.delete(access_token);
            }
        }
        //替换session
        HttpSession oldSession = request.getSession(false);
        if(oldSession != null){
            oldSession.invalidate();  //废除掉登陆前的session
        }
        return new ResultData("logout OK");
    }

    @PostMapping(value = LoginConstants.NAVIGATION_ACTION , produces=Constants.APPLICATION_JSON_UTF8)
    public Object Nav() throws IOException {
        Resource resource = new ClassPathResource("Navigation-JSON");
        JSONObject json = JSONObject.fromObject(IOUtils.toString(new FileInputStream(resource.getFile()), Charsets.UTF_8.toString()));
        System.out.println(json);
        return json;
    }


}
